package com.tongji.housediy.model
{
	import com.tongji.housediy.events.TypeXMLAdminEvent;
	import com.tongji.housediy.events.goodsEvent.FilterGoodsEvent;
	import com.tongji.housediy.events.goodsEvent.GetTypeEvent;
	import com.tongji.housediy.utils.ConstVariableInventory;
	import com.tongji.housediy.utils.LoadAdmin;
	
	import flash.events.Event;
	import flash.net.URLLoader;
	
	import mx.collections.ArrayCollection;
	import mx.collections.XMLListCollection;
	
	import org.robotlegs.mvcs.Actor;
	
	/**
	 * 
	 * @author chenchao
	 * 
	 */
	[Bindable]
	public class ListTypeModel extends Actor
	{
		public var doorXMLList:XMLListCollection;
		public var windowXMLList:XMLListCollection;
		
		// 地板 类型
		public var floorLevel1XMLList:XMLListCollection;
		public var floorLevel2XMLList:XMLListCollection;
		
		// 家具 类型
		public var furnitureLevel1XMLList:XMLListCollection;
		public var furnitureLevel2XMLList:XMLListCollection;
		
		// 墙体 类型
		public var wallLevel1XMLList:XMLListCollection;
		public var wallLevel2XMLList:XMLListCollection;
		
		// 墙体 和 踢脚线 的 index 集合
		public var wallArray:Array = [];
		public var skirtingArray:Array = [];
		
		private var doorXML:XML;
		private var windowXML:XML;
		private var floorXML:XML;
		private var funitureXML:XML;
		private var xml:XML;
		
		public function ListTypeModel()
		{
			super();
		}
		
		public function getAllXMLData():void
		{
			//var urlLoader:URLLoader = LoadAdmin.loadXML("assets/xml/typeList.xml");
			//urlLoader.addEventListener(Event.COMPLETE, loadXMLCompletedHandler);
			dispatch(new GetTypeEvent(GetTypeEvent.GET_TYPE));
		}
		
		public function loadXMLCompletedHandler(array:ArrayCollection):void
		{
			xml = new XML();
			doorXML = new XML(array[0]);
			doorXMLList = new XMLListCollection(doorXML..type);
			windowXML = new XML(array[1]);
			windowXMLList = new XMLListCollection(windowXML..type);
			floorXML = new XML(array[2]);
			floorLevel1XMLList = new XMLListCollection(floorXML..type.@name);
			funitureXML = new XML(array[3]);
			furnitureLevel1XMLList = new XMLListCollection(funitureXML.type[0]..catalogue.@name);
			
			wallLevel1XMLList = new XMLListCollection(xml.wall..type);
			wallLevel2XMLList = new XMLListCollection(xml.paint..type);
			
			// 整理 墙体 和 踢脚线 的 index
			for each (var child1:XML in xml.wallBrick..type)
			{
				if (int(child1.@index) != 0)
					wallArray.push(String(child1.@index));
			}
			for each (var child2:XML in xml.wallPaper..type)
			{
				if (int(child2.@index) != 0)
					wallArray.push(String(child2.@index));
			}
			for each (var child3:XML in xml.otherSkiring..type)
			{
				if (int(child3.@index) != 0)
					skirtingArray.push(String(child3.@index));
			}
		}
		
		/*public function loadXMLCompletedHandler(event:Event):void
		{
			xml = new XML(event.target.data);
			doorXMLList = new XMLListCollection(xml.door..type);
			windowXMLList = new XMLListCollection(xml.window..type);
			
			floorLevel1XMLList = new XMLListCollection(xml.floor..type);
			
			furnitureLevel1XMLList = new XMLListCollection(xml.room..type);
			
			wallLevel1XMLList = new XMLListCollection(xml.wall..type);
			wallLevel2XMLList = new XMLListCollection(xml.paint..type);
			
			// 整理 墙体 和 踢脚线 的 index
			for each (var child1:XML in xml.wallBrick..type)
			{
				if (int(child1.@index) != 0)
					wallArray.push(String(child1.@index));
			}
			for each (var child2:XML in xml.wallPaper..type)
			{
				if (int(child2.@index) != 0)
					wallArray.push(String(child2.@index));
			}
			for each (var child3:XML in xml.otherSkiring..type)
			{
				if (int(child3.@index) != 0)
					skirtingArray.push(String(child3.@index));
			}
		}*/
		
		public function updateLevel1FurnitureList(selectedType:String):void
		{
			switch (selectedType)
			{
				case "room":
					furnitureLevel1XMLList = new XMLListCollection(funitureXML.type[0]..catalogue.@name);
					break;
				case "function":
					furnitureLevel1XMLList = new XMLListCollection(funitureXML.type[1]..catalogue.@name);
					break;
				case "brand":
					furnitureLevel1XMLList = new XMLListCollection(funitureXML.type[2]..catalogue.@name);
					break;
			}
			dispatch(new TypeXMLAdminEvent(TypeXMLAdminEvent.SET_FURNITURE_LEVEL2_VISIBLE_FALSE));
		}
		
		public function updateLevel2FurnitureList(selectedType:String):void
		{
			furnitureLevel2XMLList = new XMLListCollection(funitureXML..catalogue.(@name == selectedType)..node);
			if (furnitureLevel2XMLList.length > 0)
				dispatch(new TypeXMLAdminEvent(TypeXMLAdminEvent.SET_FURNITURE_LEVEL2_VISIBLE_TRUE));
			else
				dispatch(new TypeXMLAdminEvent(TypeXMLAdminEvent.SET_FURNITURE_LEVEL2_VISIBLE_FALSE));
			
			// 更新 GoodsModel 中的 家具 数据源
			var selectedIndexArray:Array = [];
			
			for each (var tempXML:XML in furnitureLevel2XMLList)
			{
				selectedIndexArray.push(tempXML.@typeID.toString());
			}
			dispatch(new FilterGoodsEvent(FilterGoodsEvent.FILTER_GOODS, "furnitureList1", selectedIndexArray));
		}
		
		public function updateLevel1WallList(selectedType:String):void
		{
			switch (selectedType)
			{
				case "wall":
					wallLevel1XMLList = new XMLListCollection(xml.wall..type);
					break;
				case "skirting":
					wallLevel1XMLList = new XMLListCollection(xml.skirting..type);
					
					break;
			}
			wallLevel2XMLList = new XMLListCollection(xml.paint..type);
			ConstVariableInventory.isShowColorPicker3D = true;
		}
		
		public function updateLevel2WallList(selectedType:String):void
		{
			switch (selectedType)
			{
				case "乳胶漆":
					wallLevel2XMLList = new XMLListCollection(xml.paint..type);
					ConstVariableInventory.isShowColorPicker3D = true;
					break;
				case "墙砖":
					wallLevel2XMLList = new XMLListCollection(xml.wallBrick..type);
					ConstVariableInventory.isShowColorPicker3D = false;
					break;
				case "墙纸":
					wallLevel2XMLList = new XMLListCollection(xml.wallPaper..type);
					ConstVariableInventory.isShowColorPicker3D = false;
					break;
				case "其他":
					wallLevel2XMLList = new XMLListCollection(xml.otherSkiring..type);
					ConstVariableInventory.isShowColorPicker3D = false;
					break;
			}
			
			if (selectedType != "乳胶漆")
			{
				var selectedIndexArray:Array = [];
				
				for each (var tempXML:XML in wallLevel2XMLList)
				{
					selectedIndexArray.push(tempXML.@index.toString());
				}
				dispatch(new FilterGoodsEvent(FilterGoodsEvent.FILTER_GOODS, "wallTypeList1", selectedIndexArray));
			}
		}
		
		public function updateLevel2FloorList(selectedType:String):void
		{
			var selectedIndexArray:Array = [];
			
			switch (selectedType)
			{
				case "全部":
					dispatch(new TypeXMLAdminEvent(TypeXMLAdminEvent.SET_FURNITURE_LEVEL2_VISIBLE_FALSE));
					break;
				case "地板":
					floorLevel2XMLList = new XMLListCollection(floorXML.type[1]..node);
					break;
				case "地砖":
					floorLevel2XMLList = new XMLListCollection(floorXML.type[2]..node);
					break;
				case "地毯":
					floorLevel2XMLList = new XMLListCollection(floorXML.type[3]..node);
					break;
			}
			
			if (selectedType != "全部")
			{
				dispatch(new TypeXMLAdminEvent(TypeXMLAdminEvent.SET_FURNITURE_LEVEL2_VISIBLE_TRUE));
				
				for each (var tempXML:XML in floorLevel2XMLList)
				{
					selectedIndexArray.push(tempXML.@index.toString());
				}
			}
			dispatch(new FilterGoodsEvent(FilterGoodsEvent.FILTER_GOODS, "floorList1", selectedIndexArray));
		}
	}
}